package com.cctc.juc.demo.Athread.Bcreate.AThread;

import java.io.Closeable;
import java.io.IOException;

public class PeriodScheduler extends Thread implements Closeable {
    private long startTime;

    public PeriodScheduler() {
        super("period-scheduler");
        setDaemon(true);
        this.startTime = System.currentTimeMillis() / 1000;
    }

    @Override
    public void run() {
        while (!Thread.interrupted()) {
            long curTime = System.currentTimeMillis() / 1000;
            if (curTime - startTime >= 3) {
                System.out.println("周期调度器：每 3 秒执行一次目标任务... ...");
                startTime = curTime;
            }
        }
    }

    @Override
    public void close() throws IOException {
        startTime = Long.MAX_VALUE;
        this.interrupt();
    }
}
